package com.github.lh

import org.apache.commons.io.IOUtils
import java.awt.Color
import java.awt.image.BufferedImage
import java.awt.image.BufferedImage.TYPE_INT_ARGB
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.lang.Math.abs
import java.util.*
import javax.imageio.ImageIO
import kotlin.math.max
import kotlin.math.min

/**
 *
 * @author <a href="mailto: 393803588@qq.com">刘涵(Hanl)</a>
 * By 2018/1/6
 */
fun Int.getAlpha() = (this shr 24) and 0xFF

fun Int.getRed(): Int = (this shr 16) and 0xFF

fun Int.getGreen(): Int = (this shr 8) and 0xFF

fun Int.getBlue(): Int = (this and 0xff)

fun Int.getGray(): Int = (this.getRed() * 150 + this.getGreen() * 59 + this.getBlue() * 11 + 150) / 150

fun Int.isColorSimilar(other: Int, deviation: Int = 20): Boolean {
    if (this == other) return true
    if (abs(this.getRed() - other.getRed()) <= deviation &&
            abs(this.getGreen() - other.getGreen()) <= deviation &&
            abs(this.getBlue() - other.getBlue()) <= deviation) {
        return true
    }
    return false
}

data class Point(
        val x: Int,
        val y: Int,
        val color: Int
) {

    fun getAlpha(): Int = this.color.getAlpha()

    fun getRed(): Int = this.color.getRed()

    fun getGreen(): Int = this.color.getGreen()

    fun getBlue(): Int = this.color.getBlue()

    fun isColorSimilar(other: Point, deviation: Int = 20): Boolean =
            this.color.isColorSimilar(other.color, deviation)
}

fun getScreencap(): InputStream {
    val process = Runtime.getRuntime().exec("adb shell screencap -p")
    return process.inputStream
}

fun InputStream.saveAsPng(filePath: String) {
    val file = FileOutputStream(filePath)
    IOUtils.copy(this, file)
}

fun BufferedImage.scan(): ArrayList<Point> {
    println("width: $width, height: $height, minX: $minX, minY: $minY")
    val startY = this.height / 3
    val endY = this.height * 2 / 3

    val basePoint = this.getRGB(10, 300)

    val pixels = ArrayList<Point>(endY - startY)
    for (y in startY until endY) {
        val colors = this.getRGB(0, y, this.width, 1, null, 0, this.width)
        val colorAvg = colors.average().toInt()
        if (colors[0] == colorAvg) {
            // 在这个图形中，整条线的色彩平均值等与 起点的色彩值， 说明整条线都是一个颜色，那这个颜色是背景色，抛弃这一条线
            continue
        } else {

            colors.forEachIndexed { index, i ->
                if (!basePoint.isColorSimilar(i, 25)) {
                    if (!i.isColorSimilar(colors[max(index - 1, 0)], 10) ||
                            !i.isColorSimilar(colors[min(index + 1, this.width)], 10)) {
                        pixels += Point(index, y, i)
                    }
                }
            }
        }
    }
    println("${pixels.size}")
    return pixels
}


fun main(args: Array<String>) {
//    val image = ImageIO.read(getScreencap())
    val image = ImageIO.read(File("./autojump.png"))
    val points = image.scan()
    val newImage = BufferedImage(1080, 1920, TYPE_INT_ARGB)

    val d = newImage.createGraphics()
    d.color = Color.BLACK
    d.fillRect(0, 0, newImage.width, newImage.height)
    d.dispose()

    val pointMap = points.groupBy(Point::color, { it })

    println("${pointMap.size}")

    val file = File("./autojump_1.png")
    pointMap.forEach { k, v ->
        v.forEach { newImage.setRGB(it.x, it.y, it.color) }
    }

//    points.forEach {
//        newImage.setRGB(it.x, it.y, it.color)
//    }
    ImageIO.write(newImage, "png", file)
}